package controller

import (
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
	"week3zk-project/api/client"
	"week3zk-project/api/types"
	pb "week3zk-project/rpc/api/goods"
)

func GoodsCreate(c *gin.Context) {
	var goodsCreate types.GoodsCreateReq
	if err := c.ShouldBindJSON(&goodsCreate); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "服务器内部错误",
			"err":  err.Error(),
		})
		return
	}

	res, err := client.Client().GoodsCreate(context.Background(), &pb.GoodsCreateRequest{
		GoodsImg:    goodsCreate.GoodsImg,
		GoodsTitle:  goodsCreate.GoodsTitle,
		GoodsPrice:  float32(goodsCreate.GoodsPrice),
		GoodsStock:  goodsCreate.GoodsStock,
		SalesVolume: goodsCreate.SalesVolume,
		Status:      goodsCreate.Status,
	})
	c.JSON(http.StatusBadRequest, gin.H{
		"code": res.Code,
		"msg":  res.Msg,
		"err":  err.Error(),
	})
	c.JSON(http.StatusOK, gin.H{
		"code": res.Code,
		"msg":  res.Msg,
		"data": res.Id,
	})
}

func GoodsList(c *gin.Context) {
	var goodsList types.GoodsListReq
	if err := c.ShouldBindJSON(&goodsList); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "服务器内部错误",
			"err":  err.Error(),
		})
		return
	}
}
